package com.manage.user.controller;

import com.manage.user.entity.User;
import com.manage.user.service.UserService;
import com.manage.utils.Criteria;
import com.manage.utils.DataResult;
import com.manage.utils.PagerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Fitch
 */
@RestController
@RequestMapping("/user/api")
public class UserAjaxController {

    @Autowired
    private UserService userService;

    @PostMapping("/list")
    public PagerInfo getPage(HttpServletRequest request){
        PagerInfo pagerInfo = new PagerInfo();
        Criteria criteria = new Criteria();
        criteria.put("userName","aa");
        List<User> userList = this.userService.listUser(criteria);
        pagerInfo.setDraw(Long.valueOf("1"));
        pagerInfo.setRecordsTotal(Long.valueOf("10"));
        pagerInfo.setData(userList);
        return pagerInfo;
    };

    @PostMapping("/save")
    public User save(@RequestBody User user){

        return this.userService.saveUser(user);
    }
    @PostMapping("/checkLogin")
    public DataResult checkLogin(@RequestParam String userName,@RequestParam String password){
        List<User> userList = this.userService.getByUserName(userName);
        if(CollectionUtils.isEmpty(userList)){
            return new DataResult("用户不存在",null);
        }else {
            for(User user:userList){
                if(user.getUserName().equals(userName)&&user.getPassword().equals(password)){
                    return new DataResult(null,null);
                }else if(user.getUserName().equals(userName)&&!user.getPassword().equals(password)){
                    return new DataResult("密码错误",null);
                }
            }
            return new DataResult("用户不存在",null);
        }
    }
}
